define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ela/mainlist/index',
                    add_url: 'ela/mainlist/add',
                    //edit_url: 'ela/mainlist/edit',
                    //del_url: 'ela/mainlist/del',
                    multi_url: 'ela/mainlist/multi',
                    table: 'ela_mainlist',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        //{
                        //    field: 'operate',
                        //    width: "150px",
                        //    title: __('Operate'),
                        //    table: table,
                        //    events: Table.api.events.operate,
                        //    buttons: [
                        //        {
                        //            name: 'click',
                        //            title: __('点击执行事件'),
                        //            classname: 'btn btn-xs btn-info btn-click',
                        //            icon: 'fa fa-leaf',
                        //            // dropdown: '更多',//如果包含dropdown，将会以下拉列表的形式展示
                        //            click: function (data) {
                        //                Backend.api.addtabs("ela/deposit1/index/ids/3", "進貨單");
                        //            }
                        //        }
                        //    ],
                        //    formatter: Table.api.formatter.operate
                        //},
                        {field: 'operate2', title: __('操作'), formatter: function (value, row, index) {
                            
                            return [
                                '<a href="javascript:;" class="btn btn-xs btn-warning bdxq" data-id="' + row.receipt_id + '"data-kind="' + row.kind + '">表單詳情</a>'
                            ].join("")

                        }},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'kind', title: __('Kind'), searchList: {"0":__('Kind 0'),"1":__('Kind 1'),"2":__('Kind 2'),"3":__('Kind 3'),"4":__('Kind 4'),"5":__('Kind 5'),"6":__('Kind 6'),"7":__('Kind 7')}, formatter: Table.api.formatter.normal},
                        {field: 'stock', title: __('Stock'), searchList: {"0":__('Stock 0'),"1":__('Stock 1'),"2":__('Stock 2')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'receipt_id', title: __('Receipt_id'), operate:false},
                        {field: 'receipt', title: __('Receipt'), operate:false},
                        {field: 'receipt_number', title: __('Receipt_number'), operate:false},
                        {field: 'date', title: __('Date'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'receipt_name', title: __('Receipt_name'), operate:false},
                        {field: 'note', title: __('Note'), operate:false},
                    //    {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                    //    {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                    //    {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                    var myColumns=[];
                    $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                         myColumns.push(item.field);
                    });
                    $.cookie('fa_ela_mainlist', JSON.stringify(myColumns), { expires: 365, path: '/'});
             });
             if($.cookie('fa_ela_mainlist')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_ela_mainlist')), function(i, item) {
                  table.bootstrapTable('hideColumn', item);
                });
             }

            $(document).on("click", ".bdxq", function () {
                var receipt_id = $(this).data('id');
                var kind = $(this).data('kind');
                var title = "";
                
                //http://ela.test/admin/ela/deposit0/index/ids/3?ref=addtabs
                //http://ela.test/admin/ela/ela/deposit0/index/ids/3?ref=addtabs
                var url = "";
                if (parseInt(kind) == 0) {
                    //return [
                    //    '<a href="deposit1/index/ids/' + receipt_id + '?ref=addtabs" class="btn btn-xs btn-warning" target="_parent"  title="進貨單">表單詳情</a>'
                    //].join("")
                    url = 'deposit1/index/ids/' + receipt_id;
                    title = "進貨單";
                } else if (parseInt(kind) == 1) {
                    //return [
                    //    '<a href="productdeposit1/index/ids/' + receipt_id + '?ref=addtabs" class="btn btn-xs btn-warning" target="_parent"  title="生產入庫單">表單詳情</a>'
                    //].join("")
                    url = 'productdeposit1/index/ids/' + receipt_id;
                    title = "生產入庫單";
                } else if (parseInt(kind) == 2) {
                    //return [
                    //    '<a href="returndan1/index/ids/' + receipt_id + '?ref=addtabs" class="btn btn-xs btn-warning" target="_parent"  title="退料單">表單詳情</a>'
                    //].join("")
                    url = 'returndan1/index/ids/' + receipt_id;
                    title = "退料單";
                }  else if (parseInt(kind) == 3) {
                    //return [
                    //    '<a href="returndan09/index/ids/' + receipt_id + '?ref=addtabs" class="btn btn-xs btn-warning" target="_parent"  title="銷退單">表單詳情</a>'
                    //].join("")
                    url = 'returndan09/index/ids/' + receipt_id;
                    title = "銷退單";
                }  else if (parseInt(kind) == 4) {
                    //return [
                    //    '<a href="picking1/index/ids/' + receipt_id + '?ref=addtabs" class="btn btn-xs btn-warning" target="_parent"  title="領料單">表單詳情</a>'
                    //].join("")
                    url = 'picking1/index/ids/' + receipt_id;
                    title = "領料單";
                }  else if (parseInt(kind) == 5) {
                    //return [
                    //    '<a href="returndan08/index/ids/' + receipt_id + '?ref=addtabs" class="btn btn-xs btn-warning" target="_parent"  title="銷貨單">表單詳情</a>'
                    //].join("")
                    url = 'returndan08/index/ids/' + receipt_id;
                    title = "銷貨單";
                }  else if (parseInt(kind) == 6) {
                    //return [
                    //    '<a href="returndan11/index/ids/' + receipt_id + '?ref=addtabs" class="btn btn-xs btn-warning" target="_parent"  title="退貨單">表單詳情</a>'
                    //].join("")
                    url = 'returndan11/index/ids/' + receipt_id;
                    title = "退貨單";
                }  else {
                    //return [
                    //    '<a href="transfer1/index/ids/' + receipt_id + '?ref=addtabs" class="btn btn-xs btn-warning" target="_parent"  title="轉撥單">表單詳情</a>'
                    //].join("")
                    url = 'transfer1/index/ids/' + receipt_id;
                    title = "轉撥單";
                }

                Backend.api.addtabs('ela/' + url, title);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                infobtn: function (value, row, index) {
                    console.log(row);
                    return [
                        '<a href="ela/deposit0/index/ids/3?ref=addtabs" class="btn btn-xs btn-warning" target="_parent" >' + row.idnumber + '</a>'
                    ].join("")
                }
            }
        }
    };
    return Controller;
});